package soccerscope.daistatistic;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * @author Andr�
 * 
 * this class defines the Action Dialog and provides getter methods for the
 * dialog textfields
 */
public class ActionDialog
    implements WindowListener {

  private static int           dialogWidth        = 330;                       // dimensions
                                                                                // of
                                                                                // the
                                                                                // dialog

  private static int           dialogHigh         = 8 * 24;

  public static int            actualPlayerNumber = 1;                         // show
                                                                                // the
                                                                                // player
                                                                                // with
                                                                                // that

  // number

  public static Dialog         dialog;                                         // reference
                                                                                // to a
                                                                                // Dialog

  private static Button        dash;                                           // reference
                                                                                // buttons
                                                                                // and
                                                                                // textfields

  private static TextField     dashText;

  private static Button        move;

  private static TextField     moveText1;

  private static TextField     moveText2;

  private static Button        kick;

  private static TextField     kickText1;

  private static TextField     kickText2;

  private static Button        say;

  private static TextField     sayText;

  private static Button        searchBall;

  private static Button        turn;

  private static TextField     turnText;

  private static Button        runToBall;

  private static Button        turnNeck;

  private static TextField     turnNeckText;

  private static Button        senseBody;

  private static Button        catchBall;

  private static TextField     catchBallText;

  private static Button        viewMode;

  private static TextField     viewModeText1;

  private static TextField     viewModeText2;

  private ActionDialogListener listener           = new ActionDialogListener(); // create

  // an
  // action
  // listener

  /**
   * 
   * the constructor for the ActionDialog
   * 
   * @param SoccerScopeMenuBar
   * @param SoccerScope
   */
  public ActionDialog(
      soccerscope.SoccerScope frame) {

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    ActionDialog.dialog = new Dialog(frame, "Player actions"); // create a new
                                                                // dialog
    // with our scope as
    // "parent"
    ActionDialog.dialog.setVisible(false);
    ActionDialog.dialog.setSize(ActionDialog.dialogWidth,
        ActionDialog.dialogHigh); // set size

    int x = frame.getX() + frame.getWidth(); // get dimensions of the
    // main window
    while (x > (ge.getMaximumWindowBounds().width - ActionDialog.dialogWidth)) { // and
      // put
      // the
      // dialog
      // to
      // the
      // top
      // left
      // side
      x--;
    }

    ActionDialog.dialog.setLocation(x, frame.getY());
    ActionDialog.dialog.addWindowListener(this); // add a windowListener

    ActionDialog.dialog.setLayout(new GridLayout(8, 1, 0, 0));

    // don't show dialog at startup

    // dialog.setResizable(false); //dialog ist not resizeable

    ActionDialog.dialog.setBackground(Color.LIGHT_GRAY); // set backgound color

    // define and add all action dialog buttons and textfields

    // dash
    Panel dashPanel = new Panel();
    dashPanel.setLayout(new BorderLayout());
    ActionDialog.dash = new Button("dash(power)");
    ActionDialog.dash.setPreferredSize(new Dimension(130, 20));
    ActionDialog.dash.addActionListener(this.listener);
    ActionDialog.dash.setActionCommand("ActionDash");
    dashPanel.add("West", ActionDialog.dash);

    ActionDialog.dashText = new TextField();
    ActionDialog.dashText.setPreferredSize(new Dimension(100, 20));
    dashPanel.add(ActionDialog.dashText);
    dashPanel.setPreferredSize(new Dimension(230, 20));
    ActionDialog.dialog.add("Center", dashPanel);

    // move
    Panel movePanel = new Panel();
    movePanel.setLayout(new BorderLayout());
    ActionDialog.move = new Button("move(x,y)");
    ActionDialog.move.setPreferredSize(new Dimension(130, 20));
    ActionDialog.move.addActionListener(this.listener);
    ActionDialog.move.setActionCommand("ActionMove");
    movePanel.add("West", ActionDialog.move);

    ActionDialog.moveText1 = new TextField();
    ActionDialog.moveText1.setPreferredSize(new Dimension(100, 20));
    movePanel.add("Center", ActionDialog.moveText1);

    ActionDialog.moveText2 = new TextField();
    ActionDialog.moveText2.setPreferredSize(new Dimension(100, 20));
    movePanel.add("East", ActionDialog.moveText2);
    movePanel.setPreferredSize(new Dimension(330, 20));

    ActionDialog.dialog.add(movePanel);

    // kick
    Panel kickPanel = new Panel();
    kickPanel.setLayout(new BorderLayout());
    ActionDialog.kick = new Button("kick(power,direction)");
    ActionDialog.kick.setPreferredSize(new Dimension(130, 20));
    ActionDialog.kick.addActionListener(this.listener);
    ActionDialog.kick.setActionCommand("ActionKick");
    kickPanel.add("West", ActionDialog.kick);

    ActionDialog.kickText1 = new TextField();
    ActionDialog.kickText1.setPreferredSize(new Dimension(100, 20));

    kickPanel.add("Center", ActionDialog.kickText1);

    ActionDialog.kickText2 = new TextField();
    ActionDialog.kickText2.setPreferredSize(new Dimension(100, 20));
    kickPanel.add("East", ActionDialog.kickText2);
    kickPanel.setPreferredSize(new Dimension(330, 20));
    ActionDialog.dialog.add(kickPanel);

    // say
    Panel sayPanel = new Panel();
    sayPanel.setLayout(new BorderLayout());
    ActionDialog.say = new Button("say(msg)");
    ActionDialog.say.setPreferredSize(new Dimension(130, 20));
    ActionDialog.say.addActionListener(this.listener);
    ActionDialog.say.setActionCommand("ActionSay");
    sayPanel.add("West", ActionDialog.say);

    ActionDialog.sayText = new TextField();
    ActionDialog.sayText.setPreferredSize(new Dimension(100, 20));
    sayPanel.add("Center", ActionDialog.sayText);

    ActionDialog.searchBall = new Button("Search Ball (off)");
    ActionDialog.searchBall.setPreferredSize(new Dimension(100, 20));
    ActionDialog.searchBall.addActionListener(this.listener);
    ActionDialog.searchBall.setActionCommand("ActionSearchBall");
    sayPanel.setPreferredSize(new Dimension(330, 20));

    sayPanel.add("East", ActionDialog.searchBall);
    ActionDialog.dialog.add(sayPanel);

    // turn
    Panel turnPanel = new Panel();
    turnPanel.setLayout(new BorderLayout());
    ActionDialog.turn = new Button("turn(angle)");
    ActionDialog.turn.setPreferredSize(new Dimension(130, 20));
    ActionDialog.turn.addActionListener(this.listener);
    ActionDialog.turn.setActionCommand("ActionTurn");
    turnPanel.add("West", ActionDialog.turn);

    ActionDialog.turnText = new TextField();
    ActionDialog.turnText.setPreferredSize(new Dimension(100, 20));
    turnPanel.add("Center", ActionDialog.turnText);

    ActionDialog.runToBall = new Button("Run to Ball (off)");
    ActionDialog.runToBall.setPreferredSize(new Dimension(100, 20));
    ActionDialog.runToBall.addActionListener(this.listener);
    ActionDialog.runToBall.setActionCommand("ActionRunToBall");

    turnPanel.setPreferredSize(new Dimension(330, 20));
    turnPanel.add("East", ActionDialog.runToBall);
    ActionDialog.dialog.add(turnPanel);
    // turnNeck
    Panel turnNeckPanel = new Panel();
    turnNeckPanel.setLayout(new BorderLayout());
    ActionDialog.turnNeck = new Button("turnNeck(angle)");
    ActionDialog.turnNeck.setPreferredSize(new Dimension(130, 20));
    ActionDialog.turnNeck.addActionListener(this.listener);
    ActionDialog.turnNeck.setActionCommand("ActionTurnNeck");
    turnNeckPanel.add("West", ActionDialog.turnNeck);

    ActionDialog.turnNeckText = new TextField();
    ActionDialog.turnNeckText.setPreferredSize(new Dimension(100, 20));
    turnNeckPanel.add("Center", ActionDialog.turnNeckText);

    ActionDialog.senseBody = new Button("Sense Body");
    ActionDialog.senseBody.setPreferredSize(new Dimension(100, 20));
    ActionDialog.senseBody.addActionListener(this.listener);
    ActionDialog.senseBody.setActionCommand("ActionSenseBody");
    turnNeckPanel.add("East", ActionDialog.senseBody);

    turnNeckPanel.setPreferredSize(new Dimension(330, 20));
    ActionDialog.dialog.add(turnNeckPanel);
    // catchBall
    Panel catchBallPanel = new Panel();
    catchBallPanel.setLayout(new BorderLayout());
    ActionDialog.catchBall = new Button("catchBall(direction)");
    ActionDialog.catchBall.setPreferredSize(new Dimension(130, 20));
    ActionDialog.catchBall.addActionListener(this.listener);
    ActionDialog.catchBall.setActionCommand("ActionCatchBall");
    catchBallPanel.add("West", ActionDialog.catchBall);

    ActionDialog.catchBallText = new TextField();
    ActionDialog.catchBallText.setPreferredSize(new Dimension(100, 20));
    catchBallPanel.add("Center", ActionDialog.catchBallText);

    catchBallPanel.setPreferredSize(new Dimension(230, 20));
    ActionDialog.dialog.add(catchBallPanel);
    // viewMode
    Panel viewModePanel = new Panel();
    viewModePanel.setLayout(new BorderLayout());
    ActionDialog.viewMode = new Button("viewMode(quality, angle)");
    ActionDialog.viewMode.setPreferredSize(new Dimension(130, 20));
    ActionDialog.viewMode.addActionListener(this.listener);
    ActionDialog.viewMode.setActionCommand("ActionViewMode");
    viewModePanel.add("West", ActionDialog.viewMode);

    ActionDialog.viewModeText1 = new TextField();
    ActionDialog.viewModeText1.setPreferredSize(new Dimension(100, 20));
    viewModePanel.add("Center", ActionDialog.viewModeText1);

    ActionDialog.viewModeText2 = new TextField();
    ActionDialog.viewModeText2.setPreferredSize(new Dimension(100, 20));
    viewModePanel.add("East", ActionDialog.viewModeText2);

    viewModePanel.setPreferredSize(new Dimension(330, 20));
    ActionDialog.dialog.add(viewModePanel);

  }

  /**
   * 
   * returns true if the dialog is visible else false
   * 
   * @return boolean
   */
  public boolean dialogVisible() {

    return ActionDialog.dialog.isVisible();
  }

  /**
   * 
   * set visible or not
   * 
   * @param boolean
   */
  public static void setVisible(boolean mode) {

    ActionDialog.dialog.setVisible(mode);
  }

  /**
   * 
   * turns the on/off Buttons to off mode, used by PlayerDataDialog after a new
   * player was selected
   */
  public static void buttonsOff() {

    ActionDialog.searchBall.setLabel("Search Ball (off)");
    ActionDialog.runToBall.setLabel("Run to Ball (off)");
    Actions.goToBall = false;
    Actions.searchBallGoesOn = false;
  }

  // getter for all textfields

  /**
   * 
   * getter for the Dash TextField
   * 
   * @return String the fieldtext
   */
  public static String getDashText() {

    try {
      Integer.parseInt(ActionDialog.dashText.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.dashText.getText();
  }

  /**
   * 
   * getter for the first Move TextField
   * 
   * @return String the fieldtext
   */
  public static String getMoveText1() {

    try {
      Integer.parseInt(ActionDialog.moveText1.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.moveText1.getText();
  }

  /**
   * 
   * getter for the second Move TextField
   * 
   * @return String the fieldtext
   */
  public static String getMoveText2() {

    try {
      Integer.parseInt(ActionDialog.moveText2.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.moveText2.getText();
  }

  /**
   * 
   * getter for the first Kick TextField
   * 
   * @return String the fieldtext
   */
  public static String getKickText1() {

    try {
      Double.parseDouble(ActionDialog.kickText1.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.kickText1.getText();
  }

  /**
   * 
   * getter for the second Kick TextField
   * 
   * @return String the fieldtext
   */
  public static String getKickText2() {

    try {
      Double.parseDouble(ActionDialog.kickText2.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.kickText2.getText();
  }

  /**
   * 
   * getter for the Say TextField
   * 
   * @return String the fieldtext
   */
  public static String getSayMsg() {

    return ActionDialog.sayText.getText();
  }

  /**
   * 
   * getter for the Turn TextField
   * 
   * @return String the fieldtext
   */
  public static String getTurnDegrees() {

    try {
      Double.parseDouble(ActionDialog.turnText.getText());
    }
    catch (Exception e) {
      return "0";
    }

    return ActionDialog.turnText.getText();
  }

  /**
   * 
   * getter for the TurnNeck TextField
   * 
   * @return String the fieldtext
   */
  public static String getTurnNeckDegrees() {

    try {
      Double.parseDouble(ActionDialog.turnNeckText.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.turnNeckText.getText();
  }

  /**
   * 
   * getter for the CatchBall TextField
   * 
   * @return String the fieldtext
   */
  public static String getCatchBallText() {

    try {
      Double.parseDouble(ActionDialog.catchBallText.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.catchBallText.getText();
  }

  /**
   * 
   * getter for the first ViewMode TextField
   * 
   * @return String the fieldtext
   */
  public static String getViewModeText1() {

    try {
      Integer.parseInt(ActionDialog.viewModeText1.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.viewModeText1.getText();
  }

  /**
   * 
   * getter for the second ViewMode TextField
   * 
   * @return String the fieldtext
   */
  public static String getViewModeText2() {

    try {
      Integer.parseInt(ActionDialog.viewModeText2.getText());
    }
    catch (Exception e) {
      return "0";
    }
    return ActionDialog.viewModeText2.getText();
  }

  public static Dialog getDialog() {

    if (ActionDialog.dialog == null) {
      return null;
    }
    return ActionDialog.dialog;
  }

  public static void setTitle(String title) {

    ActionDialog.dialog.setTitle(title);
  }

  public void windowOpened(WindowEvent arg0) {

  }

  public void windowClosing(WindowEvent arg0) {

    ActionDialog.dialog.setVisible(false);

  }

  public void windowClosed(WindowEvent arg0) {

  }

  public void windowIconified(WindowEvent arg0) {

  }

  public void windowDeiconified(WindowEvent arg0) {

  }

  public void windowActivated(WindowEvent arg0) {

  }

  public void windowDeactivated(WindowEvent arg0) {

  }
}

/**
 * 
 * the ActionDialogListener handles the ActionDialog Actions
 */
class ActionDialogListener
    implements ActionListener {

  /**
   * 
   * if an action performed search the resulting action
   * 
   * @param ActionEvent
   *          the event
   */
  public void actionPerformed(ActionEvent e) {

    // String s = e.getActionCommand(); //get the ActionCommand
    // Object o = e.getSource(); //get the Action Object

    // set the actual tactic
    // StateTactic actualTactic =
    // Register.getTacticOfPlayer(ActionDialog.actualPlayerNumber);

    // a button reference, used for the on/off buttons
    // Button b;

    // check and perform the Actions

    // if (s.equals("ActionDash")){
    // actualTactic.doAction(new
    // DashAction(Integer.parseInt(ActionDialog.getDashText())));
    // }
    // if (s.equals("ActionMove")){
    // actualTactic.doAction(new
    // MoveAction(Integer.parseInt(ActionDialog.getMoveText1()),Integer.parseInt(ActionDialog.getMoveText2())));
    // }
    // if (s.equals("ActionKick")){
    // actualTactic.doAction(new
    // KickAction(Double.parseDouble(ActionDialog.getKickText1()),Double.parseDouble(ActionDialog.getKickText2())));
    // }
    // if (s.equals("ActionSay")){
    // actualTactic.doAction(new SayAction(ActionDialog.getSayMsg()));
    // }
    // if (s.equals("ActionSearchBall")){
    // b = (Button) o;
    // if (b.getLabel().equals("Search Ball (off)")){
    // b.setLabel("Search Ball (on)");
    // Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_searchBall);
    // }
    // else{
    // b.setLabel("Search Ball (off)");
    // Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_searchBall);
    // }
    // }
    // if (s.equals("ActionTurn")){
    // actualTactic.doAction(new
    // TurnAction(Integer.parseInt(ActionDialog.getTurnDegrees())));
    // }
    // if (s.equals("ActionRunToBall")){
    // b = (Button) o;
    // if (b.getLabel().equals("Run to Ball (off)")){
    // b.setLabel("Run to Ball (on)");
    // Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_goToBall);
    // }
    // else{
    // b.setLabel("Run to Ball (off)");
    // Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_goToBall);
    // }
    // }
    // if (s.equals("ActionTurnNeck")){
    // actualTactic.doAction(new
    // TurnNeckAction(Integer.parseInt(ActionDialog.getTurnNeckDegrees())));
    // }
    // if (s.equals("ActionSenseBody")){
    // actualTactic.doAction(new SenseBodyAction());
    // }
    // if (s.equals("ActionCatchBall")){
    // actualTactic.doAction(new
    // CatchAction(Integer.parseInt(ActionDialog.getCatchBallText())));
    // }
    // if (s.equals("ActionViewMode")){
    // actualTactic.doAction(new
    // ChangeViewModeAction(Integer.parseInt(ActionDialog.getViewModeText1()),Integer.parseInt(ActionDialog.getViewModeText2())));
    // }

  }

}

/**
 * $Log: ActionDialog.java,v $ Revision 1.2 2006/11/05 13:58:04 wetzker more
 * merging - CLang + MF + some states still missing
 * 
 * Revision 1.1.28.2 2006/07/31 13:36:30 endert Added Java-Doc to constructors
 * if missing, minor refactorings. Revision 1.1.28.1 2006/07/26 15:28:36 endert
 * Formatted Code Revision 1.1 2006/03/13 07:38:28 axle moved from ROOT/src for
 * Maven 2 Buildsystem and Team/Soccerscope separation
 * 
 * Revision 1.8 2006/03/07 12:46:17 endert Merge after refactoring: - New
 * State-Interface: One Method for each action type - Action Bean removed -
 * ActionFactorys for helper methods, States calculate main-Action themselves -
 * formatted code - added State-List in Monitor (Context-Menu of Player) - other
 * smaller stuff
 * 
 * Revision 1.7.2.1 2006/02/25 22:07:28 wetzker formated
 * 
 * Revision 1.7 2006/02/22 16:05:09 endert This is done: - Rebound SoccerScope
 * to Agents (can be added or removed in config-file) - Situations now all
 * extend abstract class Situation - Refactorings: ActionBean _ removed from
 * Attributes - SoccerScope will automatically connect to host given in
 * Start-Call - Trainer uses the same host as soccerscope - Removed unused stuff
 * (old confs, bat, etc.) - Code formatted (good luck when merging) - organized
 * imports - all that i forgot to mention here Revision 1.6 2005/06/23 06:04:50
 * haeder removed another unused import ;-)
 * 
 * Revision 1.5 2005/06/15 18:16:27 haeder removed unused imports
 * 
 * Revision 1.4 2005/06/15 17:03:31 wetzker removed JIAC from Head + changes in
 * framework please report bugs or shortcomings some scenarios might not work
 * because of wrong imports
 * 
 * Revision 1.3.2.1 2005/06/09 12:32:07 endert Removal of JIAC Libraries and
 * dependencies from project, restructuring the framework and improve some
 * aspects (hoping it is an improvement)
 * 
 * Revision 1.3 2005/05/11 09:57:29 andpaul changed: window handling with soccer
 * scope added: layout manager to PlayerDataDialog and ActionDialog fix: some
 * "null pointers" when an unknown player was selected
 * 
 * Revision 1.2 2005/05/05 10:12:34 haeder removed unused import
 * 
 * Revision 1.1 2005/04/20 10:39:37 endert Fresh Team Repository installation
 * from /home/e/endert/Robocup to here.
 * 
 * Revision 1.7 2005/04/13 17:05:57 andpaul no need for a soccerscopemenubar
 * parameter in actiondialog constructor
 * 
 * Revision 1.6 2005/04/02 11:08:04 andpaul documentation
 * 
 * Revision 1.5 2005/03/29 12:42:15 andpaul changes for the new MyWindowListener
 * 
 * Revision 1.4 2005/03/13 14:57:01 haeder replaced Dialog.show() by
 * Component.setVisible() because it is deprecated
 * 
 * Revision 1.3 2005/02/06 20:04:17 andpaul documentation, nothing else
 * 
 * Revision 1.2 2005/02/06 17:47:43 andpaul added try{} catch{} for textfield
 * getters
 * 
 * Revision 1.1 2005/02/06 17:13:36 andpaul setting up old gui functions for the
 * new gui
 * 
 * Revision 1.2 2005/01/29 21:35:31 andpaul better dialog location handling
 * 
 * Revision 1.1 2005/01/29 20:43:31 andpaul first steps to the "on the fly"
 * player action dialog
 * 
 * 
 */
